package com.whut.customer.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whut.common.util.R;
import com.whut.customer.entity.RoleMenu;
import com.whut.customer.service.IRoleMenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 角色-菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {

    @Autowired
    IRoleMenuService roleMenuService;

    @ApiOperation("菜单授权")
    @PostMapping("/authorization")
    public R authorization(@RequestBody RoleMenu roleMenu) {

        if(roleMenu.getMenuId()==null || roleMenu.getRoleId()==null)
            return R.failed("菜单Id或者角色id不能为空!");

        LambdaQueryWrapper<RoleMenu> eq = new LambdaQueryWrapper<RoleMenu>()
                .eq(RoleMenu::getMenuId, roleMenu.getMenuId())
                .eq(RoleMenu::getRoleId, roleMenu.getRoleId());
        if (roleMenuService.getOne(eq) != null)
            return R.failed("该授权已存在!");

        return R.ok(roleMenuService.save(roleMenu));
    }

    @ApiOperation("查询菜单授权")
    @PostMapping("/getAuthorization")
    public R<List<RoleMenu>> getAuthorization(@RequestBody RoleMenu roleMenu) {
        LambdaQueryWrapper<RoleMenu> eq = new LambdaQueryWrapper<RoleMenu>()
                .eq(roleMenu.getId() != null, RoleMenu::getId, roleMenu.getId())
                .eq(roleMenu.getMenuId() != null, RoleMenu::getMenuId, roleMenu.getMenuId())
                .eq(roleMenu.getRoleId() != null, RoleMenu::getRoleId, roleMenu.getRoleId());
        return R.ok(roleMenuService.list(eq));
    }

}
